package com.sugeun.alarm;

import com.sugeun.stopwatch.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;

public class RepeatActivity extends Activity implements OnClickListener {
	private static final String TAG = "RepeatActivity";
	
	private LinearLayout monday_group;
	private LinearLayout tuesday_group;
	private LinearLayout wednesday_group;
	private LinearLayout thursday_group;
	private LinearLayout friday_group;
	private LinearLayout saturday_group;
	private LinearLayout sunday_group;
	
	
	private CheckedTextView monday_Checkbox;
	private CheckedTextView tuesday_Checkbox;
	private CheckedTextView wednesday_Checkbox;
	private CheckedTextView thursday_Checkbox;
	private CheckedTextView friday_Checkbox;
	private CheckedTextView saturday_Checkbox;
	private CheckedTextView sunday_Checkbox;
	private Button save_btn;
	
	private boolean [] day_of_week = {false, false, false, false, false, false, false,};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repeat);
        day_of_week = getIntent().getBooleanArrayExtra("day_of_week");
        for(int i=0;i<day_of_week.length;i++){
			Log.i(TAG, " day_of_week ["+i+"] : "+day_of_week[i]);
		}
        init();
    }


	private void init() {
		// TODO Auto-generated method stub
		
		monday_group = (LinearLayout)findViewById(R.id.monday_group);
		monday_group.setOnClickListener(this);
		tuesday_group = (LinearLayout)findViewById(R.id.tuesday_group);
		tuesday_group.setOnClickListener(this);
		wednesday_group = (LinearLayout)findViewById(R.id.wednesday_group);
		wednesday_group.setOnClickListener(this);
		thursday_group = (LinearLayout)findViewById(R.id.thursday_group);
		thursday_group.setOnClickListener(this);
		friday_group = (LinearLayout)findViewById(R.id.friday_group);
		friday_group.setOnClickListener(this);
		saturday_group = (LinearLayout)findViewById(R.id.saturday_group);
		saturday_group.setOnClickListener(this);
		sunday_group = (LinearLayout)findViewById(R.id.sunday_group);
		sunday_group.setOnClickListener(this);
		save_btn = (Button)findViewById(R.id.save_btn);
		save_btn.setOnClickListener(this);
		
		
		monday_Checkbox = (CheckedTextView)findViewById(R.id.monday_Checkbox);
		tuesday_Checkbox = (CheckedTextView)findViewById(R.id.tuesday_Checkbox);
		wednesday_Checkbox = (CheckedTextView)findViewById(R.id.wednesday_Checkbox);
		thursday_Checkbox = (CheckedTextView)findViewById(R.id.thursday_Checkbox);
		friday_Checkbox = (CheckedTextView)findViewById(R.id.friday_Checkbox);
		saturday_Checkbox = (CheckedTextView)findViewById(R.id.saturday_Checkbox);
		sunday_Checkbox = (CheckedTextView)findViewById(R.id.sunday_Checkbox);
		
		monday_Checkbox.setChecked(day_of_week[0]);
		tuesday_Checkbox.setChecked(day_of_week[1]);
		wednesday_Checkbox.setChecked(day_of_week[2]);
		thursday_Checkbox.setChecked(day_of_week[3]);
		friday_Checkbox.setChecked(day_of_week[4]);
		saturday_Checkbox.setChecked(day_of_week[5]);
		sunday_Checkbox.setChecked(day_of_week[6]);
	}


	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
		case R.id.monday_group:
			if(monday_Checkbox.isChecked()){
				monday_Checkbox.setChecked(false);
				day_of_week[0] = false;
			}else{
				monday_Checkbox.setChecked(true);
				day_of_week[0] = true;
			}
			break;
		case R.id.tuesday_group:
			if(tuesday_Checkbox.isChecked()){
				tuesday_Checkbox.setChecked(false);
				day_of_week[1] = false;
			}else{
				tuesday_Checkbox.setChecked(true);
				day_of_week[1] = true;
			}
			break;
		case R.id.wednesday_group:
			if(wednesday_Checkbox.isChecked()){
				wednesday_Checkbox.setChecked(false);
				day_of_week[2] = false;
			}else{
				wednesday_Checkbox.setChecked(true);
				day_of_week[2] = true;
			}
			break;
		case R.id.thursday_group:
			if(thursday_Checkbox.isChecked()){
				thursday_Checkbox.setChecked(false);
				day_of_week[3] = false;
			}else{
				thursday_Checkbox.setChecked(true);
				day_of_week[3] = true;
			}
			break;
		case R.id.friday_group:
			if(friday_Checkbox.isChecked()){
				friday_Checkbox.setChecked(false);
				day_of_week[4] = false;
			}else{
				friday_Checkbox.setChecked(true);
				day_of_week[4] = true;
			}
			break;
		case R.id.saturday_group:
			if(saturday_Checkbox.isChecked()){
				saturday_Checkbox.setChecked(false);
				day_of_week[5] = false;
			}else{
				saturday_Checkbox.setChecked(true);
				day_of_week[5] = true;
			}
			break;
		case R.id.sunday_group:
			if(sunday_Checkbox.isChecked()){
				sunday_Checkbox.setChecked(false);
				day_of_week[6] = false;
			}else{
				sunday_Checkbox.setChecked(true);
				day_of_week[6] = true;
			}
			break;	
		case R.id.save_btn :
			Intent intent = getIntent();			
			intent.putExtra("day_of_week", day_of_week);		

			setResult(RESULT_OK, intent);
			finish();
			break;
	
		}
	}
	
}
